package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.sku.BaseSkuGallery;
import com.hoshiicloud.goods.param.sku.BaseSkuGalleryAddParam;
import com.hoshiicloud.goods.param.sku.BaseSkuGalleryQueryParam;
import com.hoshiicloud.goods.param.sku.BaseSkuGalleryUpdateParam;
import com.hoshiicloud.goods.service.BaseSkuGalleryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 基础SKU商品图片资源 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/base-sku-gallery")
@Api(value = "基础SKU商品图片", tags = {"基础SKU商品图片"})
public class BaseSkuGalleryController {
    @Autowired
    private BaseSkuGalleryService baseSkuGalleryService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询基础SKU商品图片资源
     */
    @ApiOperation(value = "分页查询基础SKU商品图片资源")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BaseSkuGalleryQueryParam param) {

        try {
            log.info("分页查询基础SKU商品图片资源");
            QueryWrapper<BaseSkuGallery> queryWrapper = new QueryWrapper<BaseSkuGallery>();
            queryWrapper.lambda().eq(param.getSkuId() != null, BaseSkuGallery::getSkuId, param.getSkuId())
                    .eq(param.getImageType() != null, BaseSkuGallery::getImageType, param.getImageType())
                    .eq(StringUtils.isNotBlank(param.getDescription()), BaseSkuGallery::getDescription, param.getDescription())
                    .eq(StringUtils.isNotBlank(param.getImage()), BaseSkuGallery::getImage, param.getImage())
                    .eq(param.getDeleted() != null, BaseSkuGallery::getDeleted, param.getDeleted());
            IPage<BaseSkuGallery> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "BaseSkuGalleryQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(baseSkuGalleryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询基础SKU商品图片资源异常", e);
            return Response.failed("分页查询基础SKU商品图片资源异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询基础SKU商品图片资源
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询基础SKU商品图片资源")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(baseSkuGalleryService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增基础SKU商品图片资源")
    public Response save(@RequestBody @Valid BaseSkuGalleryAddParam param) {
        // Bean数据转换
        BaseSkuGallery entity;
        try {
            entity = BeanUtils.covertType(param, BaseSkuGallery.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSkuGalleryService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改基础SKU商品图片资源
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改基础SKU商品图片资源")
    public Response update(@RequestBody @Valid BaseSkuGalleryUpdateParam param) {
        // Bean数据转换
        BaseSkuGallery entity;
        try {
            entity = BeanUtils.covertType(param, BaseSkuGallery.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(baseSkuGalleryService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除基础SKU商品图片资源
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除基础SKU商品图片资源")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(baseSkuGalleryService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询基础SKU商品图片资源")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(baseSkuGalleryService.getByIds(ids, StringUtils.join(ids, "")));
    }

    @ApiOperation(value = "根据BaseSkuId查询图片信息")
    @GetMapping("/listByBaseSkuId/{baseSkuId}")
    public Response listByBaseSkuId(@PathVariable("baseSkuId") Long baseSkuId) {
        return Response.data(baseSkuGalleryService.getByBaseSkuId(baseSkuId));
    }

    @ApiOperation(value = "批量更新")
    @PostMapping("/updateBatch")
    public Response updateBatch(@RequestBody OmnipotentParam<BaseSkuGallery> param) {
        return Response.data(baseSkuGalleryService.updateBatch(param));
    }
}
